import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '工作台', icon: 'el-icon-takeaway-box' }
      }
    ]
  },

  {
    path: '/manage-base-info',
    component: Layout,
    redirect: '/manage-base-info/warehouse',
    name: 'manage-base-info',
    meta: { title: '基础信息管理', icon: 'el-icon-tickets' },
    children: [
      {
        path: 'warehouse',
        name: 'warehouse',
        component: () => import('@/views/manageBaseInfo/warehouse'),
        redirect: 'warehouse',
        meta: { title: '仓库管理' },
        children: [
          {
            path: '/',
            component: () => import('@/views/manageBaseInfo/warehouse/home'),
            meta: { title: '仓库管理' }
          },
          {
            path: 'details/:id',
            component: () =>
              import('@/views/manageBaseInfo/warehouse/warehouseAdd'),
            meta: { title: '新增仓库' }
          }
        ]
      },
      {
        path: 'area',
        name: 'area',
        component: () => import('@/views/manageBaseInfo/area'),
        redirect: 'area',
        meta: { title: '库区管理' },
        children: [
          {
            path: '/',
            component: () => import('@/views/manageBaseInfo/area/home'),
            meta: { title: '库区管理' }
          },
          {
            path: 'details/:id',
            component: () => import('@/views/manageBaseInfo/area/areaAdd'),
            meta: { title: '新增库区' }
          }
        ]
      },
      {
        path: 'location',
        name: 'location',
        component: () => import('@/views/manageBaseInfo/location'),
        redirect: 'location',
        meta: { title: '库位管理' },
        children: [
          {
            path: '/',
            component: () => import('@/views/manageBaseInfo/location/home'),
            meta: { title: '库位管理' }
          },
          {
            path: 'details/:id',
            component: () =>
              import('@/views/manageBaseInfo/location/locationAdd'),
            meta: { title: '新增库位' }
          }
        ]
      },
      {
        path: 'location-view',
        name: 'location-view',
        component: () => import('@/views/manageBaseInfo/locationView'),
        meta: { title: '库位视图' }
      },
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/manageBaseInfo/goods'),
        redirect: 'goods',
        meta: { title: '货品管理' },
        children: [
          {
            path: '/',
            component: () => import('@/views/manageBaseInfo/goods/home'),
            meta: { title: '货品管理' }
          },
          {
            path: 'details/:id',
            component: () => import('@/views/manageBaseInfo/goods/goodsAdd'),
            meta: { title: '新增货品' }
          }
        ]
      },
      {
        path: 'goods-type',
        name: 'goods-type',
        component: () => import('@/views/manageBaseInfo/goodsType'),
        meta: { title: '货品类型管理' }
      }
    ]
  },
  {
    path: '/manage-business',
    component: Layout,
    redirect: '/manage-business/goods-owner',
    name: 'manage-business',
    meta: { title: '商务管理', icon: 'el-icon-suitcase' },
    children: [
      {
        path: 'goods-owner',
        name: 'goods-owner',
        component: () => import('@/views/manageBusiness/goodsOwner'),
        redirect: 'goods-owner',
        meta: { title: '货主管理' },
        children: [
          {
            path: '/',
            component: () => import('@/views/manageBusiness/goodsOwner/home'),
            meta: { title: '货主管理' }
          },
          {
            path: 'details/:id',
            component: () => import('@/views/manageBusiness/goodsOwner/goodsOwnerAdd'),
            meta: { title: '新增货主' }
          },
          {
            path: 'detail/:id',
            component: () => import('@/views/manageBusiness/goodsOwner/goodOwnerDetail'),
            meta: { title: '货主详情' }
          }
        ]
      },
      {
        path: 'task-picking',
        name: 'task-picking',
        component: () => import('@/views/manageBusiness/taskPicking'),
        meta: { title: '承运商管理' }
      }
    ]
  },
  {
    path: '/manage-storage',
    component: Layout,
    redirect: '/manage-storage/list',
    name: 'manage-storage',
    meta: { title: '库内管理', icon: 'el-icon-house' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/manageStorage/list'),
        meta: { title: '盘点单' }
      },
      {
        path: 'list-task',
        name: 'list-task',
        component: () => import('@/views/manageStorage/listTask'),
        meta: { title: '盘点任务' }
      },
      {
        path: 'storage-lose-profit',
        name: 'storage-lose-profit',
        component: () => import('@/views/manageStorage/storageLoseProfit'),
        meta: { title: '库存损益' }
      },
      {
        path: 'latest-storage',
        name: 'latest-storage',
        component: () => import('@/views/manageStorage/latestStorage'),
        meta: { title: '实时库存' }
      }
    ]
  },
  {
    path: '/manage-storage-in',
    component: Layout,
    redirect: '/manage-storage-in/list',
    name: 'manage-storage-in',
    meta: { title: '入库管理', icon: 'el-icon-bottom-right' },
    children: [
      {
        path: 'list',
        name: 'list-in',
        component: () => import('@/views/manageStorageIn/list'),
        meta: { title: '入库单' }
      },
      {
        path: 'task-receive',
        name: 'task-receive',
        component: () => import('@/views/manageStorageIn/taskReceive'),
        meta: { title: '收货任务' }
      },
      {
        path: 'task-add',
        name: 'task-add',
        component: () => import('@/views/manageStorageIn/taskAdd'),
        meta: { title: '上架任务' }
      }
    ]
  },
  {
    path: '/manage-storage-out',
    component: Layout,
    redirect: '/manage-storage-out/list-out',
    name: 'manage-storage-out',
    meta: { title: '入库管理', icon: 'el-icon-top-right' },
    children: [
      {
        path: 'list-out',
        name: 'list-out',
        component: () => import('@/views/manageStorageOut/listOut'),
        meta: { title: '出库单' }
      },
      {
        path: 'task-picking',
        name: 'task-picking-out',
        component: () => import('@/views/manageStorageOut/taskPicking'),
        meta: { title: '拣货任务' }
      },
      {
        path: 'manage-task-transfer',
        name: 'manage-task-transfer',
        component: () => import('@/views/manageStorageOut/manageTaskTransfer'),
        meta: { title: '交接任务' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// 有新增与修改俩功能
const titleList = ['新增仓库', '新增库区', '新增库位', '新增货品', '新增货主']

router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title // 浏览器标签
  if (to.path !== '/404' && to.path !== '/login') {
    const title = setLinkTitle(to)
    store.commit('tagsView/add', { link: to.path, title })
  } // 标签页
  next()
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

function setLinkTitle(to) {
  const title = to.meta.title
  if (
    titleList.indexOf(title) === -1 ||
    to.path.match('[^/]+(?!.*/)')[0] === 'null'
  ) { return title } else {
    return title.replace(/新增/, '编辑')
  }
}
